export const setupHttp = (app) => {
	let httpConfig = {
		baseUrl: uni.$config.env.baseURL,
		originalData: true,
		// loadingText: '努力加载中~',
		loadingTime: 800,
		// 设置自定义头部content-type
		// header: {
		// 	'content-type': 'xxx'
		// }
		// ......
	}
	
	//请求拦截
	let requestInterceptor = function(request){
		if(uni.$store.userStore.token){
			request.header.Authorization = "Bearer " + uni.$store.userStore.token
		}
		
		return request;
	}
	
	function showError(response){
		uni.showToast({
			icon:response.message.length >= 7 ? "none" : "error",
			title:response.message
		})
	}
	
	//拦截原始响应
	let responseOriginDataInterceptor = function(response){
		if(response.statusCode == 200){
			 return responseInterceptor(response.data)
		}
		showError(response.data)
		return false
	}
	//拦截响应
	let responseInterceptor = function(response){
		if(response.code == uni.$config.constant.SUCCESS){
			return response
		}
	
		if(response.message){
			 showError(response)
		}
		return false
	}
	
	
	
	let http = app.config.globalProperties.$u.http
	
	http.setConfig(httpConfig);
	http.interceptor.request = requestInterceptor
	http.interceptor.response  = responseOriginDataInterceptor
}

